﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability_Management.Domain;
using Traceability_Management.ErrorCode;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Application.Command.SlaughtsWay;

namespace Traceability_Management.Read.Api.Application.CommandHandler.SlaughtsWay
{
    public class CheckSlauCompanyImagesCommandHandler : IRequestHandler<CheckSlauCompanyImagesCommand, ApiResult<int>>
    {
        IBaseReponsitory<Slaughts> Slaughts;

        public CheckSlauCompanyImagesCommandHandler(IBaseReponsitory<Slaughts> Slaughts)
        {
            this.Slaughts = Slaughts;
        }

        public async Task<ApiResult<int>> Handle(CheckSlauCompanyImagesCommand request, CancellationToken cancellationToken)
        {
            var Uplist = await Slaughts.GetQuery().Where(c => c.Rabit_Batch == request.Batch).FirstOrDefaultAsync();

            if (Uplist != null)
            {
                if (request.CheckImages != null)
                {
                    Uplist.Companyimg = request.CheckImages;
                }
                await Slaughts.UpdateAsync(Uplist);
                return new ApiResult<int>
                {
                    Code = ApiEnum.Success,
                    Message = "保存成功",
                    Data = 1
                };
            }
            else
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "请设置养殖信息",
                    Data = -1
                };
            }
        }
    }
}
